


#include "log/log2.h"
#include "BSP_Sleep.h"

#include "unity.h"
#include "unity_fixture.h"

#include "lfs/lfs_config.h"

#define  DEBUG_PRINTF(...)	LOG(__VA_ARGS__)
#define PTHRU_ON_OFF            (1 << 6)   /* NC_REG Pass-through bit */

int lfs_format2(void);

TEST_GROUP(FS);


TEST_SETUP(FS)
{

}

TEST_TEAR_DOWN(FS)
{

}

TEST(FS, ReadWrite)
{
	lfs_file_t file;
    // mount the filesystem
    int err = lfs_setup();
	if(err){
		FAIL("lfs_setup error");
	}

	// create a new file
	lfs_file_open(&lfs, &file, "hello", LFS_O_CREAT | LFS_O_RDWR);

	// write to the file
	const char *data = "Hello LittleFS!";
	lfs_file_write(&lfs, &file, data, strlen(data));

	// remember the storage is not updated until the file is closed successfully
	lfs_file_close(&lfs, &file);

	// release any resources we were using
	lfs_unmount(&lfs);

	// mount the filesystem
	err = lfs_setup();

    // read current count
    uint8_t read_buffer[32] = {0};
    lfs_file_open(&lfs, &file, "hello", LFS_O_RDWR | LFS_O_CREAT);
    lfs_file_read(&lfs, &file, read_buffer, sizeof(read_buffer));
    // remember the storage is not updated until the file is closed successfully
    lfs_file_close(&lfs, &file);

    LOG2_INF("read_buffer: %s\n", read_buffer);
	lfs_format2();
    // release any resources we were using
    lfs_unmount(&lfs);
}

TEST(FS, ReadWriteLarge)
{
	lfs_file_t file;
	uint8_t buffer[256] = {0};
	for(int i = 0; i < 256; i++){
		buffer[i] = i;
	}
    // mount the filesystem
    int err = lfs_setup();
	if(err){
		FAIL("lfs_setup error");
	}

	// create a new file
	lfs_file_open(&lfs, &file, "hello", LFS_O_CREAT | LFS_O_RDWR);

	// write to the file
	lfs_file_write(&lfs, &file, buffer, sizeof(buffer));

	// remember the storage is not updated until the file is closed successfully
	lfs_file_close(&lfs, &file);

	// release any resources we were using
	lfs_unmount(&lfs);

	// mount the filesystem
	err = lfs_setup();

    // read current count
    uint8_t read_buffer[256] = {0};
    lfs_file_open(&lfs, &file, "hello", LFS_O_RDWR | LFS_O_CREAT);
    lfs_file_read(&lfs, &file, read_buffer, sizeof(read_buffer));
    // remember the storage is not updated until the file is closed successfully
    lfs_file_close(&lfs, &file);
	for(int i = 0; i < 256; i++){
		TEST_ASSERT_EQUAL_INT(read_buffer[i], i);
	}
    // LOG2_INF("read_buffer: %s\n", read_buffer);
	lfs_format2();
    // release any resources we were using
    lfs_unmount(&lfs);
}